package com.tarena.test.charging.es;

import com.tarena.charging.order.OrderApplication;
import com.tarena.charging.order.pojo.entity.ChargingProcessEntity;
import com.tarena.charging.order.repository.ChargingProcessEntityRepository;
import java.math.BigDecimal;
import java.util.Optional;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

@SpringBootTest(classes= OrderApplication.class)
public class ESSpringBoot {
    @Autowired
    private RestHighLevelClient client;
    @Autowired
    private ChargingProcessEntityRepository esRepository;

    @Test
    public void test() throws Exception{
        System.out.println(client);
    }
    @Test
    public void addDoc(){
        ChargingProcessEntity entity=new ChargingProcessEntity();
        entity.setId(2L);
        entity.setBillId("bill0020");
        entity.setUserId(20);
        entity.setStationId(20);
        entity.setGunId(20);
        entity.setChargingCapacity(10F);
        entity.setTotalCost(new BigDecimal(10));
        entity.setTemperture(60F);
        entity.setIsFull(false);
        //新增一个文档
        ChargingProcessEntity save = esRepository.save(entity);
        System.out.println(save);
    }
    //删除
    @Test
    public void deleteDoc(){
        esRepository.deleteById(2L);
    }
    //查询 find search
    @Test
    public void searchDoc(){
        Optional<ChargingProcessEntity> entity = esRepository.findById(1l);
        //底层封装文档也不是将doc直接返回 会有一些 元数据
        ChargingProcessEntity findEntity = entity.get();
        System.out.println(findEntity);
        //find查询 要不就是无参 底层是match all 能做 分页排序 byId单个查询
        //根据mapping映射的属性 integer long double float keyword termQuery text matchquery
        //根据提供的对象非空属性 拼接 对应的查询条件 类型不同条件也不同
        ChargingProcessEntity searchEntity=new ChargingProcessEntity();
        searchEntity.setBillId("bill0020");
        searchEntity.setIsFull(false);
        searchEntity.setId(1L);
        //where bill_id="bill0020" and is_full=false
        PageRequest request = PageRequest.of(1, 10);
        String[] fields={"id"};
        Page<ChargingProcessEntity> entities = esRepository.searchSimilar(searchEntity,fields , request);
        entities.get().forEach(e-> System.out.println(e));

    }
    @Test
    public void testBillId(){
        Long id=1l;
        ChargingProcessEntity searchEntity=new ChargingProcessEntity();
        searchEntity.setId(id);
        String[] args={"id"};
        Pageable pageable=PageRequest.of(1,10);
        Page<ChargingProcessEntity> entities = esRepository.searchSimilar(searchEntity, args, pageable);
        entities.get().forEach(e-> System.out.println(e));
    }

}
